package com.jeecms.wicool.manager.product.impl;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.common.hibernate3.Updater;
import com.jeecms.common.page.Pagination;
import com.jeecms.wicool.dao.product.ProductDao;
import com.jeecms.wicool.dao.product.ProductInnerRelationDao;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.product.ProductCate;
import com.jeecms.wicool.entity.product.ProductInnerRelation;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.product.ProductCateMng;
import com.jeecms.wicool.manager.product.ProductMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ProductMngImpl implements ProductMng {

    private ProductDao dao;

    @Autowired
    public void setDao(ProductDao dao) {
        this.dao = dao;
    }

    @Transactional(readOnly = true)
    public Pagination getPage(Integer productCateId, String productName, int pageNo, int pageSize, CmsUser user) {
        Pagination page = dao.getPage(productCateId, productName, pageNo, pageSize, user);
        return page;
    }
    
    public Pagination getPageForVip(Integer productCateId, String productName, int pageNo, int pageSize, CmsUser user){
    	 Pagination page = dao.getPageForVip(productCateId, productName, pageNo, pageSize, user);
         return page;
    }

    @Override
    public Product findById(Integer id) {
        Product entity = dao.findById(id);
        return entity;
    }

    @Override
    public Product update(Product bean, Integer shopId, Integer productCateId) {

        ShopInfo shop = shopMng.findById(shopId);
        ProductCate pc = productCateMng.findById(productCateId);

        bean.setShop(shop);
        bean.setProductCate(pc);
        Updater<Product> updater = new Updater<Product>(bean);
        bean = dao.updateByUpdater(updater);
        return bean;
    }

    @Override
    public Product save(Product bean, Integer shopId, Integer productCateId) {

        ShopInfo shop = shopMng.findById(shopId);
        ProductCate pc = productCateMng.findById(productCateId);
        bean.setShop(shop);
        bean.setProductCate(pc);
        dao.save(bean);
        return bean;
    }

    @Override
    public Product deleteById(Integer id) {
        Product bean = dao.deleteById(id);
        return bean;
    }

    @Override
    public Product[] deleteByIds(Integer[] ids) {
        Product[] beans = new Product[ids.length];
        for (int i = 0, len = ids.length; i < len; i++) {
            beans[i] = deleteById(ids[i]);
        }
        return beans;
    }

    @Override
    public Integer getMaxItemNo() {
        return dao.getMaxItemNo();
    }

    @Override
    public List<Product> getAll(Integer productCateId) {
        return dao.getAll(productCateId);
    }

    @Autowired
    private WiShopInfoMng shopMng;
    @Autowired
    private ProductCateMng productCateMng;
    
    @Autowired
    private ProductInnerRelationDao productInnerRelationDao;

	@Override
	public void saveProductInnerRelation(Integer[] id, Integer[] productId,
			String[] type, String[] value) {
		for (int i=0 ; i < 2 ; i++) {
    		
    		if("withFood".equals(type[i])){
    			if(id[i] == null){
    				ProductInnerRelation withFood = new ProductInnerRelation();
            		withFood.setProductId(productId[i]);
            		withFood.setType("withFood");
            		withFood.setValue(value[i]);
            		productInnerRelationDao.save(withFood);
            		System.out.println(withFood);
    			}else{
    				ProductInnerRelation withFood = new ProductInnerRelation();
    				withFood.setId(id[i]);
            		withFood.setProductId(productId[i]);
            		withFood.setType("withFood");
            		withFood.setValue(value[i]);
            		
            		Updater<ProductInnerRelation> updater = new Updater<ProductInnerRelation>(withFood);
            		productInnerRelationDao.updateByUpdater(updater);
    			}
    			
    		}else if("soup".equals(type[i])){
    			if(id[i] == null){
	    			ProductInnerRelation soup = new ProductInnerRelation();
	        		soup.setProductId(productId[i]);
	        		soup.setType("soup");
	        		soup.setValue(value[i]);
	        		productInnerRelationDao.save(soup);
	        		System.out.println(soup);
    			}else{
    				ProductInnerRelation soup = new ProductInnerRelation();
    				soup.setId(id[i]);
    				soup.setProductId(productId[i]);
    				soup.setType("soup");
    				soup.setValue(value[i]);
            		
            		Updater<ProductInnerRelation> updater = new Updater<ProductInnerRelation>(soup);
            		productInnerRelationDao.updateByUpdater(updater);
    			}
    		}
		}
	}
}